/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "workerloadgpx.h"
#include "layertrack.h"


WorkerLoadGpx::WorkerLoadGpx(QXmlStreamReader* x, LayerTrack* t) :
    WorkerProcess(2)
{
    track = t;
    stream = x;
}

WorkerLoadGpx::~WorkerLoadGpx()
{
}

bool WorkerLoadGpx::do_stuff()
{
    int cur_pt = 0;
    int cur_wp = 0;
    int stepper = 10;

    track->data.clear();
    track->waypoints.clear();

    // Since there might many titles and descriptions (one for each point or even worse)
    // we only want to read the first one of each
    bool read_title = false;
    bool read_desc = false;

    sendUpdate( tr("No points added yet"), 0, 0, 0, 0 );
    sendUpdate( tr("No waypoints added yet"), 1, 0, 0, 0 );

    while (!stream->atEnd() && !stream->hasError() && !please_terminate )
    {
        if ( stream->isStartElement() )
        {
            if (stream->name() == "trkpt")
            {
                cur_pt++;
                if ( cur_pt == 100 )
                    stepper = 100;
                else if ( cur_pt == 1000 )
                    stepper = 1000;
                else if ( cur_pt == 10000 )
                    stepper = 10000;
                TrackPoint new_point(stream);
                track->appendTrackPoint( new_point );
//                points->append( new_point );
                if ( (cur_pt % stepper) == 0 )
                {
                    sendUpdate( tr("Added point n.%1").arg(cur_pt), 0, 0, 0, 0 );
                }
            }
            else if ( (stream->name() == "name") && !read_title )
            {
                stream->readNext();
                title = stream->text().toString();
                read_title = true;
            }
            else if ( (stream->name() == "desc") && !read_desc )
            {
                stream->readNext();
                description = stream->text().toString();
                read_desc = true;
            }
            else if ( (stream->name() == "wpt") )
            {
                cur_wp++;
                TrackWaypoint new_wp(stream);
                track->appendWaypoint( new_wp );
//                waypoints->append( new_wp );

                sendUpdate( tr("Added waypoint n.%1").arg(cur_wp), 1, 0, 0, 0 );
            }
        }
        stream->readNext();
        qApp->processEvents( QEventLoop::AllEvents );
        if ( please_terminate )
            return false;
    }
    if ( please_terminate )
        return false;
    return true;
}

void WorkerLoadGpx::getContents( QString& t, QString& d)
{
    t = title;
    d = description;
}
